﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.ServiceProject;

namespace ZBJF.Warranty.BLL
{
    public partial class ServiceProjectService
    {
        #region 获取单个服务项目 + ServiceProject LoadByID(int id)
        /// <summary>
        /// 获取单个服务项目
        /// </summary>
        /// <param name="id">服务项目ID</param>
        /// <returns></returns>
        public ServiceProject LoadByID(int id)
        {
            return LoadEntities(o => o.ID == id).FirstOrDefault();
        }
        #endregion

        #region 判断是否存在 + bool IsExist(string projectName, int id = 0)
        /// <summary>
        /// 判断是否存在
        /// </summary>
        /// <param name="productName">服务项目名称</param>
        /// <param name="id">需要排除的服务项目ID</param>
        /// <returns></returns>
        public bool IsExist(string projectName, int id = 0)
        {
            var expressionWhere = PredicateExtensions.True<ServiceProject>();
            if (!string.IsNullOrEmpty(projectName))
            {
                expressionWhere = expressionWhere.And(o => o.ProjectName == projectName);
            }
            if (id > 0)
            {
                expressionWhere = expressionWhere.And(o => o.ID != id);
            }
            return LoadEntities(expressionWhere).Any();
        }
        #endregion

        #region 分页查询 + IQueryable<ServiceProject> GetPagingList(ServiceProjectModel model, out int total)
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="model">过滤条件</param>
        /// <param name="total">返回总条数</param>
        /// <returns></returns>
        public IQueryable<ServiceProject> GetPagingList(ServiceProjectModel model, out int total)
        {
            var expressionWhere = PredicateExtensions.True<ServiceProject>();
            if (!string.IsNullOrEmpty(model.ProjectName))
            {
                expressionWhere = expressionWhere.And(t => t.ProjectName.Contains(model.ProjectName));
            }
            if (model.ServiceProductType > 0)
            {
                expressionWhere = expressionWhere.And(t => t.ServiceProductType == model.ServiceProductType);
            }
            return LoadPagerEntities<ServiceProject>(model.PageIndex, model.iDisplayLength, out total, expressionWhere, model.iSorts);
        }
        #endregion

        #region 获取服务项目名称 + string GetProjectName(int serviceProjectId)
        /// <summary>
        /// 获取服务项目名称
        /// </summary>
        /// <param name="serviceProjectId">服务项目ID</param>
        /// <returns></returns>
        public string GetProjectName(int serviceProjectId)
        {
           ServiceProject re = LoadByID(serviceProjectId);
            if (re == null)
                return "";
            else
                return re.ProjectName;
        }
        #endregion

        public IQueryable<ServiceProject> GetServiceProjectListByInsuranceOrderid(int insuranceOrderid)
        {
            return ServiceFactory.ServiceProjectServiceInstance.LoadEntities(n => true);
        }
    }
}
